﻿using System.Web.Security;

namespace Controller2.Services.Admin
{
	public class MembershipService : IMembershipService
	{
		private readonly MembershipProvider _provider;
		private readonly IEmailService _emailService;

		public MembershipService()
			: this(null, null)
		{
		}

		public MembershipService(MembershipProvider provider, IEmailService emailService)
		{
			_provider = provider ?? Membership.Provider;
			_emailService = emailService ?? new EmailService("relay-hosting.secureserver.net", 25);
		}

		public int MinPasswordLength { get { return _provider.MinRequiredPasswordLength; } }

		public int MinNonAplhaNumerics { get { return _provider.MinRequiredNonAlphanumericCharacters; } }

		public bool ValidateUser(string userName, string password)
		{
			return _provider.ValidateUser(userName, password);
		}

		public MembershipCreateStatus CreateUser(string userName, string password, string email)
		{
			int totalRecords;
			_provider.GetAllUsers(0, 1, out totalRecords);

			MembershipCreateStatus status;
			MembershipUser user =_provider.CreateUser(userName, password, email, null, null, true, null, out status);

			if(totalRecords==0)
			{
				// This was the first user....make them an admin
				//
				if(!Roles.RoleExists("Admin"))
				{
					Roles.CreateRole("Admin");
				}
				Roles.AddUserToRole(userName, "Admin");
			}
			return status;
		}

		public bool ChangePassword(string userName, string oldPassword, string newPassword)
		{
			MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
			return currentUser.ChangePassword(oldPassword, newPassword);
		}

		public bool ResetPassword(string userName, string adminUserName, out string newPassword)
		{
			MembershipUser user = GetUser(userName, false);

			// Generate a new password
			//
			newPassword = user.ResetPassword();

			string body =
				@"

Your password to the Heartland Farm Sanctuary website has
been reset by the site admin. Your new credentials are
below. Please log in and reset your password as soon as
possible.

username = " + user.UserName + @"
password = " + newPassword + @"

site url: http://heartlandfarmsanctuary.org

reset by: " + adminUserName;


			return _emailService.Send(
											EmailService.DoNotReplyEmailAddress,
											"HFS Password Reset",
											"Your HFS password has been reset...",
											body,
											false,
											user.Email
											);

		}

		public MembershipUserCollection GetAllUsers()
		{
			int totalRecords;
			return GetAllUsers(0, int.MaxValue, out totalRecords);
		}

		public MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
		{
			return _provider.GetAllUsers(pageIndex, pageSize, out totalRecords);
		}

		public MembershipUser GetUser(string userName, bool userIsOnline)
		{
			return _provider.GetUser(userName, userIsOnline);
		}

		public void UpdateUser(MembershipUser user)
		{
			_provider.UpdateUser(user);
		}

		public bool  DeleteUser(string userName, bool deleteAllRelatedData)
		{
			return _provider.DeleteUser(userName, deleteAllRelatedData);
		}
	}
}